package com.example.administrator.hqq.fragment;


import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.adapter.FragmentsAdapter;
import com.example.administrator.hqq.base.MyFragemnt;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.fragment.dialog.ContactFragment;
import com.example.administrator.hqq.utils.SharedPrefsStrListUtil;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.rong.imkit.RongIM;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imkit.model.UIConversation;
import io.rong.imlib.model.Conversation;

/**
 * A simple {@link Fragment} subclass.
 */
public class DialogueFragment extends MyFragemnt {


    @BindView(R.id.message_text)
    TextView messageText;
    @BindView(R.id.message)
    AutoRelativeLayout message;
    @BindView(R.id.post_view)
    View postView;
    @BindView(R.id.normal_auto)
    AutoRelativeLayout normalAuto;
    @BindView(R.id.contacts)
    AutoRelativeLayout contacts;
    @BindView(R.id.discover_view)
    View discoverView;
    @BindView(R.id.contacts_auto)
    AutoRelativeLayout contactsAuto;
    @BindView(R.id.group_search_button)
    ImageView groupSearchButton;
    @BindView(R.id.search_auto)
    AutoRelativeLayout searchAuto;
    @BindView(R.id.discover_vp)
    ViewPager discoverVp;
    Unbinder unbinder1;
    private List<Fragment> fragments;
    private String TAG = this.getClass().getSimpleName();

    public DialogueFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_dialogue, container, false);
        unbinder1 = ButterKnife.bind(this, inflate);
        initview();
        findGroup();
        setOnclick();
        return inflate;
    }

    private void setOnclick() {
        RongIM.setConversationListBehaviorListener(new RongIM.ConversationListBehaviorListener() {
            @Override
            public boolean onConversationPortraitClick(Context context, Conversation.ConversationType conversationType, String s) {
//                Log.e(TAG, "onConversationClick: "+s );
//                SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.id)).put( LoongsConfig.Conversation.ConversationID,s);
//                if (conversationType.equals(Conversation.ConversationType.PRIVATE))
//                {
//                    SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.id)).put(LoongsConfig.Conversation.ChatConversationType, LoongsConfig.Conversation.PRIVATE);
//                }
//                else{
//                    SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.id)).put(LoongsConfig.Conversation.ChatConversationType, LoongsConfig.Conversation.GROUP);
//                }
                return true;
            }

            @Override
            public boolean onConversationPortraitLongClick(Context context, Conversation.ConversationType conversationType, String s) {
                return false;
            }

            @Override
            public boolean onConversationLongClick(Context context, View view, UIConversation uiConversation) {
                return false;
            }

            @Override
            public boolean onConversationClick(Context context, View view, UIConversation uiConversation) {
                Log.e(TAG, "onConversationClick: "+uiConversation.getConversationTargetId() );
                SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).put( LoongsConfig.Conversation.ConversationID,uiConversation.getConversationTargetId());
                if (uiConversation.getConversationType().equals(Conversation.ConversationType.PRIVATE))
                {
                    SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).put(LoongsConfig.Conversation.ChatConversationType, LoongsConfig.Conversation.PRIVATE);
                }
                else{
                    SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).put(LoongsConfig.Conversation.ChatConversationType, LoongsConfig.Conversation.GROUP);
                }
                return false;
            }
        });
    }

    private void findGroup() {

    }

    private void initview() {
        fragments = new ArrayList<>();
        ContactFragment unConventionalityFragment = new ContactFragment();
        fragments.add(initComversationList());
        fragments.add(unConventionalityFragment);
        FragmentsAdapter fragmentsAdapter = new FragmentsAdapter(getChildFragmentManager(), fragments);
        discoverVp.setAdapter(fragmentsAdapter);
        normalAuto.setSelected(true);
        discoverVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    normalAuto.setSelected(true);
                    contactsAuto.setSelected(false);
                } else {
                    contactsAuto.setSelected(true);
                    normalAuto.setSelected(false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private ConversationListFragment initComversationList() {
        ConversationListFragment conversationListFragment = new ConversationListFragment();
        Uri uri = Uri.parse("rong://" + getContext().getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话，该会话聚合显示
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//设置群组会话，该会话非聚合显示
                .build();
        conversationListFragment.setUri(uri);
        return conversationListFragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder1.unbind();
    }

    @OnClick({R.id.normal_auto, R.id.contacts_auto, R.id.search_auto})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.normal_auto:
                normalAuto.setSelected(true);
                contactsAuto.setSelected(false);
                discoverVp.setCurrentItem(0);
                break;
            case R.id.contacts_auto:
                normalAuto.setSelected(false);
                contactsAuto.setSelected(true);
                discoverVp.setCurrentItem(1);
                break;
            case R.id.search_auto:
                break;
        }
    }
}
